/*
 * @Author: your name
 * @Date: 2022-02-28 09:45:33
 * @LastEditTime: 2022-04-03 20:09:40
 * @LastEditors: Please set LastEditors
 * @Description: 用户模块接口
 * @FilePath: \blog-system-serve\Routes\auth.js
 */
const db = require('../Model/model')
const express = require('express')
const auth = express.Router()
const jsonToken = require('jsonwebtoken')
// const multer = require('multer')
const path = require('path')
// const file = require('fs')
// const uploadConfig = multer({
//   dest: path.join(__dirname, '../', 'public'),
// })
// const publicPath = path.join(__dirname, '../', 'public')
// auth.post('/uploadHead', uploadConfig.single('avatar'), (req, res) => {
//   const { userId } = req.body
//   file.rename(
//     req.file.path,
//     `${publicPath}/${req.file.originalname}`,
//     (err) => {
//       if (err) throw err
//       else {
//         // 文件存储成功
//         const querySql = `select * from user_info WHERE userId = '${userId}'`
//         // db(querySql, (err, result)=>{
//         //   const updateSql = `UPDATE user_info set picUrl = 'http://localhost:5000/${req.file.originalname}' WHERE userId = '${userId}'`;
//         //   db(updateSql, (err,req) =>{
//         //     console.log(err);
//         //     res.status(200).send({
//         //       data: "上传成功"
//         //     })
//         //   })
//         // })
//       }
//     },
//   )
// })

/**
 * @description: 获取用户信息接口
 * @param {*}
 * @return {*}
 */
auth.get('/self', (req, res) => {
  const token = (req.headers.authorization || '').split(' ').pop()
  const { username } = jsonToken.decode(token)
  const querySql = `SELECT username, userId,hobby,nickname,telphone,email, sex,area,realname,picUrl FROM user_info WHERE username = "${username}"`
  db(querySql, (err, reuslt) => {
    if (err) {
      res.send({
        err,
      })
      return err
    }
    res.status(200).json({
      ...reuslt[0],
    })
  })
})
/**
 * @description: 获取所有用户相关接口
 * @param {*}
 * @return {*}
 */
auth.get('/userlist', (req, response) => {
  const { pageSize, pageNum = 10 } = req.query
  const startIndex = (pageSize - 1) * pageNum
  const endIndex = Number(startIndex) + Number(pageNum)
  const querySql = `select * from user_info LIMIT ${startIndex}, ${pageNum}`
  db(querySql, (err, reuslt = []) => {
    const totalSql = `SELECT COUNT(*) AS total FROM user_info  `
    db(totalSql, (err, res) => {
      response.status(200).json({
        total: res[0].total,
        content: reuslt.map((d) => {
          const item = d
          delete item.password
          return item
        }),
      })
    })
  })
})
/**
 * @description: 更新用户个人信息
 * @param {*}
 * @return {*}
 */
auth.post('/update/userlist', (req, res) => {
  const {
    userId,
    telphone,
    nickname,
    email,
    realname,
    hobby,
    sex,
    area,
    picUrl
  } = req.body
  const updateSql = `
   UPDATE user_info SET
   telphone = '${telphone ? telphone : null}',
   nickname = '${nickname}', 
   email = '${email}', 
   realname = '${realname}', 
   hobby = '${hobby}',
   sex = '${sex}',
   area = '${area}',
   picUrl = '${picUrl}'
   WHERE userId = ${userId}`
  db(updateSql, (err, reuslt) => {
    if (reuslt.affectedRows === 1) {
      res.status(200).json({
        ...req.body,
      })
    }
  })
})
module.exports = auth
